package com.quanyan.financialSystem.service.place.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.financialSystem.entity.db.TbBizStatements;
import com.quanyan.financialSystem.entity.db.TbBizStatementsExample;
import com.quanyan.financialSystem.entity.vo.resp.CashFinishFinishDetailInner;
import com.quanyan.financialSystem.entity.vo.resp.RespCashFinishFinancial;
import com.quanyan.financialSystem.entity.vo.resp.RespCashFinishFinancialDetail;
import com.quanyan.financialSystem.entity.vo.resp.RespPlaceProfitReport;
import com.quanyan.financialSystem.mapper.CashFinishFinancialMapper;
import com.quanyan.financialSystem.mapper.TbBizStatementsMapper;
import com.quanyan.financialSystem.service.place.CashFinishOrderFinancialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author xian-guo
 * @email 498152332@qq.com
 * @date 2016/10/22.
 */
@Service("cashFinishOrderFinancialService")
public class CashFinishOrderFinancialServiceImpl implements CashFinishOrderFinancialService {

    @Autowired
    private CashFinishFinancialMapper cashFinishFinancialMapper;
    /**
     * 现金结算单
     * @param paramMap
     * @return
     */
    @Override
    public APIResponse<PageObj<List<RespCashFinishFinancial>>> getCashFinishOrder(Map<String, Object> paramMap) {
        List<RespCashFinishFinancial> respCashFinishFinancialList=cashFinishFinancialMapper.getCashFinishFinancial(paramMap);
        if (respCashFinishFinancialList.size()>0){
            int totalCount=cashFinishFinancialMapper.getCashFinishCount(paramMap);
            Integer impor= (Integer) paramMap.get("impor");
            if (null !=impor && impor==1){
                PageObj<List<RespCashFinishFinancial>> result=PageObj.create(totalCount, (int) paramMap.get("pageNum"), totalCount, respCashFinishFinancialList);
                return APIResponse.returnSuccess(result);
            }
            PageObj<List<RespCashFinishFinancial>> result=PageObj.create(totalCount, (int) paramMap.get("pageNum"), (int) paramMap.get("pageSize"), respCashFinishFinancialList);
            return APIResponse.returnSuccess(result);
        }
        return APIResponse.returnSuccess();
    }

    /**
     * 现金结算单明细
     * @param paramMap
     * @return
     */
    @Override
    public APIResponse<RespCashFinishFinancialDetail> getCashFinishOrderDetail(Map<String,Object> paramMap) {
        RespCashFinishFinancialDetail respCashFinishFinancialDetail=cashFinishFinancialMapper.getCashFinishFinancialDetail(paramMap);
        if (null !=respCashFinishFinancialDetail){
            PageObj<List<CashFinishFinishDetailInner>> result=PageObj.create(cashFinishFinancialMapper.getCashFinishFinancialDetailCount(paramMap),
                    (int) paramMap.get("pageNum"), (int) paramMap.get("pageSize"), respCashFinishFinancialDetail.getCashFinishFinishDetailInnerList());
            respCashFinishFinancialDetail.setCashFinishFinishDetailInnerList(null);
            respCashFinishFinancialDetail.setListPageObj(result);
            return APIResponse.returnSuccess(respCashFinishFinancialDetail);
        }
        return APIResponse.returnFail("查询数据失败");
    }

    /**
     * 保存备注按钮
     * @param paramMap
     * @return
     */
    @Override
    public APIResponse saveCashFinishRemarkButton(Map<String, Object> paramMap) {
       int save=cashFinishFinancialMapper.saveCashFinishRemark(paramMap);
        return save>0?APIResponse.returnSuccess("保存成功"):APIResponse.returnFail("保存失败");
    }

    @Autowired
    private TbBizStatementsMapper tbBizStatementsMapper;
    /**
     * 确认按钮
     * @param paramMap
     * @return
     */
    @Override
    public APIResponse confirmCashFinishRemarkButton(Map<String, Object> paramMap) {
        TbBizStatementsExample tbBizStatementsExample=new TbBizStatementsExample();
        tbBizStatementsExample.createCriteria().andStatementIdEqualTo((String) paramMap.get("statementId"));
        List<TbBizStatements> tbBizStatementsList=tbBizStatementsMapper.selectByExample(tbBizStatementsExample);
        if (tbBizStatementsList.size()>0){
            TbBizStatements tbBizStatements=tbBizStatementsList.get(0);
            if (tbBizStatements.getStatementStatus()==3){//该状态不允许提交保存
                return APIResponse.returnFail("该结算单已经确认结算");
            }else{
                int confirm=cashFinishFinancialMapper.confirmCashFinishRemark(paramMap);
                return confirm>0?APIResponse.returnSuccess("保存成功"):APIResponse.returnFail("保存失败");
            }
        }
        return APIResponse.returnFail("保存失败");
    }


}
